﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MathEasy.src.Editing.Controls;
using MathEasy.src.LMD.Core;

namespace MathEasy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PreviewWindow pwindow;

        public MainWindow()
        {
            System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)1;
            InitializeComponent();
            ProgressRefresher.setProgressBarToRefresh(pbar1);

        }

        private void reloadPreview(object sender)
        {
            try
            {
                LMD_Document lmdDoc = new LMD_Document(mbox.getDocument());
                pwindow.loadHTMLToPreview(lmdDoc.toHTML(LMD_Document.RenderingMode.LOCAL));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void PreviewDocument(object sender, RoutedEventArgs e)
        {
            try
            {
                pwindow = new PreviewWindow();
                pwindow.ReloadRequested += new ReloadRequestedHandler(reloadPreview);
                pwindow.Closed += new EventHandler(optimizator);
                LMD_Document lmdDoc = new LMD_Document(mbox.getDocument());
                pwindow.loadHTMLToPreview(lmdDoc.toHTML(LMD_Document.RenderingMode.LOCAL));
                pwindow.Show();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void optimizator(object sender, EventArgs e)
        {
            pwindow = null;
            GC.Collect();
        }

        private void ShowAboutMsg(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Author: Hidić Adnan, ETF Sarajevo, 2013.\nE-Mail: ahidic2@etf.unsa.ba\nThe code is open source and can be modified and distributed freely. Make sure you mention the original author! :)", "About author", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
